package com.cnswhy.cloud.server.merchant.controller.customer;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.request.PageParams;
import com.cnswhy.cloud.common.core.utils.DateUtils;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.core.dao.WalletTransDtlMapper;
import com.cnswhy.cloud.server.core.dto.CustomerWalletTransDtlPageDTO;
import com.cnswhy.cloud.server.core.dto.WalletTransDtlPageDTO;
import com.cnswhy.cloud.server.core.dto.WalletTransDtlSaveDTO;
import com.cnswhy.cloud.server.core.dto.WalletTransDtlUpdateDTO;
import com.cnswhy.cloud.server.core.entity.CustomerWalletTransDtl;
import com.cnswhy.cloud.server.core.entity.CustomerWalletTransExcelDtl;
import com.cnswhy.cloud.server.core.entity.WalletTransDtl;
import com.cnswhy.cloud.server.core.service.WalletTransDtlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/walletTransDtl")
@Api(value = "WalletTransDtl", tags = "")
//@PreAuth(replace = "walletTransDtl:")
public class WalletTransDtlController extends SuperController<WalletTransDtlService, Long, WalletTransDtl, WalletTransDtlPageDTO, WalletTransDtlSaveDTO, WalletTransDtlUpdateDTO> {
    @Autowired
    WalletTransDtlService walletTransDtlService;
    @Autowired
    WalletTransDtlMapper walletTransDtlMapper;
    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "分页列表查询+账户")
    @PostMapping(value = "/customerWalletTransDtlPage")
    //@SysLog(value = "'分页列表查询:第' + #params?.current + '页, 显示' + #params?.size + '行'", response = false)
    //@PreAuth("hasPermit('{}view')")
    public R<IPage<CustomerWalletTransDtl>> customerWalletTransDtlPage(@RequestBody @Validated PageParams<CustomerWalletTransDtlPageDTO> params) {
        // 处理参数
        Map<String, String> map = params.getMap();
        if(StringUtils.isNotBlank(map.get("createTime_st"))){
            params.getModel().setCreateDateSt(DateUtils.getStartTime(map.get("createTime_st")));
        }
        if(StringUtils.isNotBlank(map.get("createTime_ed"))){
            params.getModel().setCreateDateEnd(DateUtils.getStartTime(map.get("createTime_ed")));
        }
        IPage<CustomerWalletTransDtl> page = params.buildPage();
        //query1(params, page, null);
        page = walletTransDtlMapper.getCustomerWalletTransDtl(page,params.getModel());
        return success(page);
    }
    /**
     * 导出Excel
     *
     * @param params   参数
     * @param request  请求
     * @param response 响应
     * @throws IOException
     */
    @ApiOperation(value = "导出Excel")
    //@RequestMapping(value = "/export1", method = RequestMethod.POST, produces = "application/octet-stream")
    @RequestMapping(value = "/export1", method = RequestMethod.POST)
    //@SysLog("'导出Excel:'.concat(#params.map[" + NormalExcelConstants.FILE_NAME + "]?:'')")
    //@PreAuth("hasPermit('{}export')")
    public void exportExcel1(@RequestBody @Validated PageParams<CustomerWalletTransDtlPageDTO> params, HttpServletRequest request, HttpServletResponse response) {
        ExportParams exportParams = getExportParams1(params);
        Map<String, String> m1ap = params.getMap();
        if(StringUtils.isNotBlank(m1ap.get("createTime_st"))){
            params.getModel().setCreateDateSt(DateUtils.getStartTime(m1ap.get("createTime_st")));
        }
        if(StringUtils.isNotBlank(m1ap.get("createTime_ed"))){
            params.getModel().setCreateDateEnd(DateUtils.getStartTime(m1ap.get("createTime_ed")));
        }
        List<CustomerWalletTransExcelDtl> customerWalletTransDtlList = walletTransDtlMapper.exportCustomerWalletTransDtl1(params.getModel());

        Map<String, Object> map = new HashMap<>(5);
        map.put(NormalExcelConstants.DATA_LIST, customerWalletTransDtlList);
        map.put(NormalExcelConstants.CLASS, CustomerWalletTransExcelDtl.class);
        map.put(NormalExcelConstants.PARAMS, exportParams);
        String fileName = params.getMap().getOrDefault(NormalExcelConstants.FILE_NAME, "临时文件");
        map.put(NormalExcelConstants.FILE_NAME, fileName);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
        //ExcelKit.$Export(CustomerWalletTransDtl.class, response).downXlsx(list, false);

    }

    /**
     * 构建导出参数
     *
     * @param params 分页参数
     * @return
     */
    public ExportParams getExportParams1(PageParams<CustomerWalletTransDtlPageDTO> params) {

        String title = params.getMap().get("title");
        String type = params.getMap().getOrDefault("type", ExcelType.XSSF.name());
        String sheetName = params.getMap().getOrDefault("sheetName", "SheetName");

        ExcelType excelType = ExcelType.XSSF.name().equals(type) ? ExcelType.XSSF : ExcelType.HSSF;
        return new ExportParams(title, sheetName, excelType);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<WalletTransDtl> walletTransDtlList = list.stream().map((map) -> {
            WalletTransDtl walletTransDtl = WalletTransDtl.builder().build();
            return walletTransDtl;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(walletTransDtlList));
    }
}
